package com.dpiinc.TechPage

import android.graphics.Bitmap
import android.graphics.Canvas
import com.beiji.lib.pen.cache.BitmapCacheModel
import com.beiji.lib.pen.cache.NoteCacheModel
import com.beiji.lib.pen.cache.PenCacheManager
import java.io.File

/**
 * 离线数据合并
 * Created by X on 2018/7/21.
 */
object OfflineDataMergeHelper {

    fun merge(desNoteId: String, srcNoteId: String, completeCallback: (() -> Unit)? = null): NoteCacheModel {
        return PenCacheManager.instance.loadCache(desNoteId) { destCacheModel ->
            PenCacheManager.instance.loadCache(srcNoteId) { srcCacheModel ->
                mergeStrokes(destCacheModel, srcCacheModel)
                mergeBitmap(destCacheModel, srcCacheModel)
                destCacheModel.endCache()
                //清除缓存，删除本地缓存目录
                PenCacheManager.instance.removeCache(srcNoteId, true)
                completeCallback?.invoke()
            }
        }
    }

    private fun mergeStrokes(destCacheModel: NoteCacheModel, srcCacheModel: NoteCacheModel) {
        val strokeList = srcCacheModel.readStrokesFromFileSynchronously()
        strokeList.forEach {
            destCacheModel.cachePenStroke(it)
        }

    }

//    private fun mergeStrokes(desNoteId: String, srcNoteId: String) {
//        PenCacheManager.instance.loadCache(desNoteId) { destCacheModel ->
//            val strokeList = PenCacheManager.instance.loadCache(srcNoteId).readStrokesFromFileSynchronously()
//            strokeList.forEach {
//                destCacheModel.cachePenStroke(it)
//            }
//            destCacheModel.endCache()
//        }
//    }

    /**
     * 合并笔迹图片
     */
    private fun mergeBitmap(destCacheModel: NoteCacheModel, srcCacheModel: NoteCacheModel) {
        val canvas = Canvas()
        srcCacheModel.bitmapCacheList.forEach { srcBitmap ->
            var newPage = true
            destCacheModel.bitmapCacheList.forEach { destBitmap ->
                if (srcBitmap.realPageNumber == destBitmap.realPageNumber && srcBitmap.bookId == destBitmap.bookId) {
                    newPage = false
                    destBitmap.apply {
                        canvas.setBitmap(this.bitmap)
                        canvas.drawBitmap(srcBitmap.bitmap, 0f, 0f, null)
                    }
                }
            }
            if (newPage) {
                val pageIndex = destCacheModel.bitmapCacheList.size
                destCacheModel.bitmapCacheList.add(BitmapCacheModel(pageIndex, srcBitmap.realPageNumber, srcBitmap.bitmap, srcBitmap.bookId, srcBitmap.bitmapFile))
            }
        }
    }

//    /**
//     * 合并笔迹图片
//     */
//    private fun mergeBitmap(desNoteId: String, srcNoteId: String) {
//        val canvas = Canvas()
//        PenCacheManager.instance.loadCache(srcNoteId) { cacheModel ->
//            cacheModel.bitmapCacheList.forEach { srcBitmap ->
//                val desBitmapPath = PenCacheManager.instance.getStrokeBitmapPathByPageNum(desNoteId, srcBitmap.realPageNumber)
//                var desBitmap: Bitmap? = null
//                desBitmapPath?.let {
//                    val options = BitmapFactory.Options()
//                    options.inMutable = true
//                    desBitmap = BitmapFactory.decodeFile(it, options)
//                }
//                desBitmap?.let {
//                    canvas.setBitmap(it)
//                    canvas.drawBitmap(srcBitmap.bitmap, 0f, 0f, null)
//                    saveBitmap(it, desBitmapPath!!)
//                }
//            }
//        }
//    }

    private fun saveBitmap(bitmap: Bitmap, path: String) {
        val file = File(path)
        if (file.exists()) {
            file.delete()
        }
        val outputStream = file.outputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream)
        outputStream.close()
    }
}